<?php

namespace App\Transformers\Mengborun;

use App\Models\Mengborun\Order;
use League\Fractal\ParamBag;
use League\Fractal\TransformerAbstract;
use Carbon\Carbon;

class OrderTransformer extends TransformerAbstract
{
//    protected $availableIncludes = ['user',  'recentComments'];

    public function transform(Order $order)
    {
//        return $order->attributesToArray();

        $goods = $order->goods;
        $img = '';
        $stock = '';
        if(isset($goods->id))
        {
            $img = $goods->img;
            $stock = $goods->stock;
        }
        $createTime = '';
        if(!empty($order->created_at))
        {
            $createTime  = $order->created_at->toDateTimeString();
        }
        return [
            'id' => $order->id,
            'user_id' => $order->user_id,
            'order_no' => $order->order_no,
            'order_status' => $order->order_status,
            'goods_id' => $order->goods_id,
            'name' => $order->name,
            'point' => $order->point,
            'stock' => $stock,
            'goods_img' => $img,
            'created_at' => $createTime
//            'created_at' => $order->created_at->toDateTimeString(),

//            'created_at' => date('',$order->created_at)

//            'updated_at' => $order->updated_at->toDateTimeString(),
        ];
    }

//  如果有包含子对象，可重写此方法
//    public function includeUser(Post $order)
//    {
//        if (! $post->user) {
//            return $this->null();
//        }
//
//        return $this->item($post->user, new UserTransformer());
//    }


    /**
     * 列表加载列表不是一件很好的事情，因为dingo的预加载机制
     * 自动预加载include的参数, 所以会读取所有帖子的所有评论
     * 所以可以增加一个recentComments, 增加一个limit条件
     * 但是依然不够完美.
     */
//    public function includeRecentComments(Post $post)
//    {
//        // 倒叙
//        $comments = $post->recentComments->sortByDesc('id');
//
//        return $this->collection($comments, new CommentTransformer())
//            ->setMeta([
//                'count' => $comments->count(),
//            ]);
//    }
}
